﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace AutoServey
{
    class XmlExport
    {
        public void writePanoLocation(string xmlPath, string panoLocation)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);

            XmlElement rootElem = doc.DocumentElement;
            XmlNode taskNode = rootElem.SelectSingleNode("//Task");
            if (rootElem.GetElementsByTagName("PanoLocation").Count > 0)
            {
                rootElem.GetElementsByTagName("PanoLocation")[0].InnerText = panoLocation;
            }
            else
            {
                createNodeWithInnerText(doc, (XmlElement)taskNode, "PanoLocation", panoLocation);
            }
            doc.Save(xmlPath);
        }

        /// <summary>
        /// 创建带innertext节点
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="parent"></param>
        /// <param name="nodeName"></param>
        /// <param name="innerText"></param>
        /// <returns></returns>
        public XmlElement createNodeWithInnerText(XmlDocument doc, XmlElement parent, string nodeName, string innerText)
        {
            XmlElement childNode = doc.CreateElement(nodeName);
            childNode.InnerText = innerText;
            parent.AppendChild(childNode);
            return childNode;
        }
    }
}
